<template>
  <view class="pages">
    <h-nav-bar :fixed="true" backgroundColor="#F5EFFD" title="提醒设置"></h-nav-bar>
    <view class="section">
      <view class="sec_meal">
        <view class="sec_meal_box" v-for="(item, index) in messageList" v-show="item?.type < 7">
          <view class="sec_meal_list">
            <view class="sec_meal_list_l">
              <image src="@/static/yunchan/weight/zao.png" v-show="item?.type == 1"></image>
              <image src="@/static/yunchan/weight/wan.png" v-show="item?.type == 2"></image>
              <image src="@/static/yunchan/weight/wu.png" v-show="item?.type == 3"></image>
            </view>
            <view class="sec_meal_list_c">
              <view class="sec_meal_list_c_t">{{ item?.name }}</view>
              <view class="sec_meal_list_c_b">{{ item?.crontab_time }}</view>
            </view>
            <view class="sec_meal_list_r" @click="getIndex(item, index)" v-if="item?.open">
              <up-switch
                v-model="item.open"
                activeColor="#A28AFF"
                size="17"
                inactiveColor="#BBBBBB"
                @change="changeSex"
              ></up-switch>
            </view>
          </view>
        </view>
      </view>
      <view class="sec_meal">
        <view class="sec_meal_box" v-for="(item, index) in messageList" v-show="item?.type == 7">
          <view class="sec_meal_list">
            <view class="sec_meal_list_l">
              <image src="@/static/yunchan/weight/yuna.png"></image>
            </view>
            <view class="sec_meal_list_c">
              <view class="sec_meal_list_c_t">{{ item?.name }}提醒</view>
              <view class="sec_meal_list_c_b">{{ item?.crontab_time }}</view>
            </view>
            <view class="sec_meal_list_r" @click="getIndex(item)" v-if="item?.open">
              <up-switch
                v-model="item.open"
                activeColor="#A28AFF"
                size="17"
                inactiveColor="#BBBBBB"
                @change="changeSex"
              ></up-switch>
            </view>
          </view>
        </view>
      </view>
    </view>
  </view>
</template>

<script setup lang="ts">
import { ref, reactive, nextTick } from 'vue'
import { formatDate } from '@/utils/utils'
import { navigateTo, showToast, navigateBack } from '@/utils'
import dayjs from 'dayjs'
import { getOpenPush } from '@/api/pregnancy/index'
import {
  onLoad,
  onPullDownRefresh,
  onReachBottom,
  onReady,
  onShow,
  onPageScroll,
  onUnload,
  onHide,
} from '@dcloudio/uni-app'
import { ycDietOpenPushMessage, ycDietMessageList } from '@/api/weight/index.js'
import { useMemberStore } from '@/stores'
const login = ref(null) // 登录组件实例
const store = useMemberStore()
var token = store?.token
const statusHeight = ref(0)
getSystemInfo()

function getSystemInfo() {
  uni.getSystemInfo({
    success: (e) => {
      statusHeight.value = e.statusBarHeight * 2
    },
  })
}
const messageList = ref([])
const newsRres = ref({})

const warnShow = ref(false)
onLoad((o) => {
  getDetail()
})
onShow(() => {
  getQuanxian()
})
async function getDetail() {
  //获取当前权限状态
  var res = await ycDietMessageList()
  var list = res.data
  for (var i in list) {
    if (list[i].openStatus == 0) {
      list[i].open = false
    } else {
      list[i].open = true
    }
  }
  var arr = [list[0], list[3], list[1], list[4], list[2], list[5], list[6]]
  messageList.value = arr
}
var openDetail = {}
var openIndex = 0

function getIndex(item, index) {
  openDetail = item
  openIndex = index
}

function changeSex(e) {
  if (!warnShow.value) {
    setTimeout(async function () {
      uni.showModal({
        title: '提示',
        content: '是否前往打开通知权限',
        success: (res) => {
          if (res.confirm) {
            openTongZhi()
          } else if (res.cancel) {
            console.log('用户点击取消')
          }
        },
      })
      messageList.value[openIndex].open = false
    }, 1)
  } else {
    setMessage(e)
  }
}
async function setMessage(e) {
  setTimeout(async function () {
    var res = await ycDietOpenPushMessage({
      operation: e ? 1 : 0,
      type: openDetail.type,
      id: openDetail.id,
    })
    uni.showToast({
      title: res.msg,
      duration: 2000,
    })
  }, 300)
}
//消息通知权限
function getQuanxian() {
  let platform = uni.getSystemInfoSync().platform //首先判断app是安卓还是ios
  /* #ifdef APP-PLUS */
  if (platform == 'ios') {
    var UIApplication = plus.ios.import('UIApplication')
    var app = UIApplication.sharedApplication()
    var enabledTypes = 0
    if (app.currentUserNotificationSettings) {
      var settings = app.currentUserNotificationSettings()
      enabledTypes = settings.plusGetAttribute('types')
      if (enabledTypes == 0) {
        warnShow.value = false
        uni.showModal({
          title: '提示',
          content: '是否前往打开通知权限',
          success: (res) => {
            if (res.confirm) {
              openTongZhi()
            } else if (res.cancel) {
              console.log('用户点击取消')
            }
          },
        })
        //如果enabledTypes = 0 就是通知权限没有开启
      } else {
        warnShow.value = true
      }
    }
    plus.ios.deleteObject(settings)
  } else if (platform == 'android') {
    var main = plus.android.runtimeMainActivity()
    var pkName = main.getPackageName()
    var uid = main.getApplicationInfo().plusGetAttribute('uid')
    console.log(uid)
    var NotificationManagerCompat = plus.android.importClass(
      'android.support.v4.app.NotificationManagerCompat',
    )
    if (NotificationManagerCompat == null) {
      NotificationManagerCompat = plus.android.importClass(
        'androidx.core.app.NotificationManagerCompat',
      )
    }
    var areNotificationsEnabled = NotificationManagerCompat.from(main).areNotificationsEnabled()
    if (areNotificationsEnabled) {
      warnShow.value = true
    } else {
      warnShow.value = false
      uni.showModal({
        title: '提示',
        content: '是否前往打开通知权限',
        success: (res) => {
          if (res.confirm) {
            openTongZhi()
          } else if (res.cancel) {
            console.log('用户点击取消')
          }
        },
      })
    }
  }
  /* #endif */
}
//前往打开消息通知
function openTongZhi() {
  uni.openAppAuthorizeSetting({
    success(res) {
      console.log(res)
    },
  })
}
</script>

<style lang="scss">
page {
  background: #f6f6f6;
}
</style>
<style lang="scss" scoped>
.pages {
  min-height: 100vh;
  background: linear-gradient(180deg, #f5effd 0%, #f6f6f6 100vh);
}

.section {
  box-sizing: border-box;
  padding: 0 24rpx;
  .sec_meal {
    background: #ffffff;
    border-radius: 16rpx;
    box-sizing: border-box;
    margin-bottom: 20rpx;
    padding: 0 30rpx;
    .sec_meal_box {
      padding: 15rpx 0;
    }
    .sec_meal_box:nth-of-type(2n) {
      border-bottom: 1rpx solid #f6f6f6;
    }
    .sec_meal_box:last-of-type {
      border-bottom: none;
    }
    .sec_meal_list {
      overflow: hidden;
      padding: 15rpx 0;
      .sec_meal_list_l {
        float: left;
        width: 68rpx;
        height: 68rpx;
        border-radius: 50%;
        image {
          width: 100%;
          height: 100%;
          padding: 10rpx;
          box-sizing: border-box;
          border-radius: 50%;
          background: #f5f0fc;
        }
      }
      .sec_meal_list_c {
        float: left;
        margin-left: 20rpx;
        .sec_meal_list_c_t {
          font-family: PingFang SC, PingFang SC;
          font-weight: 500;
          font-size: 28rpx;
          color: #333333;
          line-height: 40rpx;
        }
        .sec_meal_list_c_b {
          font-family: PingFang SC, PingFang SC;
          font-weight: 500;
          font-size: 18rpx;
          color: #777777;
          line-height: 25rpx;
          margin-top: 3rpx;
        }
      }
      .sec_meal_list_r {
        float: right;
        display: flex;
        flex-direction: row;
        /* 子元素横向排列 */
        justify-content: center;
        /* 相对父元素水平居中 */
        align-items: center;
        height: 68rpx;
      }
    }
  }
}
</style>
